//
//  MainTools.swift
//  jrw
//
//  Created by cabinzhao on 5/23/17.
//  Copyright © 2017 武汉玖信普惠金融信息服务有限公司. All rights reserved.
//

import Foundation

class MainTools: NSObject {
    
    static let shared = MainTools()
    //用户数据
    var userModel:UserModel? {
        set {
            if let tempModel = newValue {
                UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: tempModel), forKey: "UserModel")
            } else {
                UserDefaults.standard.removeObject(forKey: "UserModel")
            }
        }
        get {
            if let tempData = UserDefaults.standard.data(forKey: "UserModel") {
                return NSKeyedUnarchiver.unarchiveObject(with: tempData) as? UserModel
            } else {
                return nil
            }
        }
    }
    
    //点赞数据
    private var dzData:[String]? {
        set {
            if let tempModel = newValue {
                UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: tempModel), forKey: "dzData")
            } else {
                UserDefaults.standard.removeObject(forKey: "dzData")
            }
        }
        get {
            if let tempData = UserDefaults.standard.data(forKey: "dzData") {
                return NSKeyedUnarchiver.unarchiveObject(with: tempData) as? [String]
            } else {
                return nil
            }
        }
    }
    
    func isDzByUser(uid:String, cid:String) -> Bool {
        return self.dzData?.contains(uid+cid) ?? false
    }
    
    func saveDzDataByUser(uid:String, cid:String) {
        var dzTpData = self.dzData ?? [String]()
        dzTpData.append(uid+cid)
        self.dzData = dzTpData
    }
    
    //获得当前的window对象
    class var window:UIWindow {
        if let delegate = UIApplication.shared.delegate as? AppDelegate {
            return delegate.window ?? UIWindow()
        }
        return UIWindow()
    }
    
    //Toast提示
    class func makeToast(message:String?) {
        MainTools.window.makeToast(message)
    }
    
    class func initSbVc<T>(sbName:String, AnyClass:T.Type) -> T {
        var strClassName:String = "\(NSStringFromClass(AnyClass as! AnyClass).components(separatedBy: ".").last!)"
        strClassName = String(strClassName.first!).lowercased() + strClassName.dropFirst()
        return UIStoryboard(name: sbName, bundle: nil).instantiateViewController(withIdentifier: strClassName) as! T
    }
    
    //将图片压缩到指定大小，并返回bas64字符串
    class func converImageToBase64String(myImage:UIImage?) -> String {
        if myImage == nil {
            return ""
        }
        var data = UIImageJPEGRepresentation(myImage!, 1.0)
        if (data?.count)! > 5 * 1024 * 1024 {
            data = UIImageJPEGRepresentation(myImage!, 0.002)
        } else if (data?.count)! > 1024 * 1024 {
            data = UIImageJPEGRepresentation(myImage!, 0.005)
        } else if (data?.count)! > 512 * 1024 {
            data = UIImageJPEGRepresentation(myImage!, 0.01)
        } else if (data?.count)! > 256 * 1024 {
            data = UIImageJPEGRepresentation(myImage!, 0.25)
        } else if (data?.count)! > 128 * 1024 {
            data = UIImageJPEGRepresentation(myImage!, 0.5)
        }
        return "data:image/jpeg;base64,\((data?.base64EncodedString() ?? ""))" 
    }

    
}
